package redis

import (
	"time"

	"code.byted.org/bytertc/whiteboard_server/biz/pkg/kv"
	"code.byted.org/kv/goredis"
)

var _ kv.Pipeline = (*PipelineImpl)(nil)

type PipelineImpl struct {
	*goredis.Pipeline
}

func (p *PipelineImpl) Incr(key string) {
	p.Pipeline.Incr(key)
}

func (p *PipelineImpl) IncrBy(key string, value int64) {
	p.Pipeline.IncrBy(key, value)
}

func (p *PipelineImpl) DecrBy(key string, value int64) {
	p.Pipeline.DecrBy(key, value)
}

func (p *PipelineImpl) Set(key, value string, expiration time.Duration) {
	p.Pipeline.Set(key, value, expiration)
}

func (p *PipelineImpl) SetNX(key, value string, expiration time.Duration) {
	p.Pipeline.SetNX(key, value, expiration)
}

func (p *PipelineImpl) Get(key string) {
	p.Pipeline.Get(key)
}

func (p *PipelineImpl) Del(key ...string) {
	p.Pipeline.Del(key...)
}

func (p *PipelineImpl) RPush(key string, values []string) {
	ifs := make([]interface{}, 0, len(values))
	for index := range values {
		ifs = append(ifs, values[index])
	}
	p.Pipeline.RPush(key, ifs...)
}

func (p *PipelineImpl) Expire(key string, expiration time.Duration) {
	p.Pipeline.Expire(key, expiration)
}

func (p *PipelineImpl) HSet(key, field, value string) {
	p.Pipeline.HSet(key, field, value)
}

func (p *PipelineImpl) HMSet(key string, fields map[string]interface{}) {
	p.Pipeline.HMSet(key, fields)
}

func (p *PipelineImpl) HDel(key, field string) {
	p.Pipeline.HDel(key, field)
}

func (p *PipelineImpl) Exec() ([]kv.Cmder, error) {
	rawCmder, err := p.Pipeline.Exec()
	if err != nil {
		return nil, err
	}
	cmder := make([]kv.Cmder, len(rawCmder))
	for index := range rawCmder {
		cmder[index] = rawCmder[index]
	}
	return cmder, nil
}
